{include file="header/index" /}
<meta charset="UTF-8">
<style>
	.form-control {
		height: 33px;
	}
	.Role-assignment-tc{
		margin-top: 0 !important;
	}
</style>

<body>
	<div class="container privilegeUserList pb40">
		<h2 class="list-title">用户组列表</h2>
		<div class="addUser">
			<div class="row">
				<div class="col-lg-2 col-md-2 	col-sm-3">
					<a href="{:url('Authgroup/add')}" class="btn btn-default addUser-btn">添加用户组</a>
				</div>
				<!-- <div class="col-lg-4  col-md-4 	col-sm-6">
						<div class="input-group">
							<input type="text" class="form-control borBox" placeholder="输入需要查询的用户名">
							<span class="input-group-btn">
				        <button class="btn btn-primary borBox" type="button">搜索</button>
				      </span>
						</div>
						/input-group
					</div> -->
				<!-- /.col-lg-6 -->
			</div>
			<!-- /.row -->
		</div>
		<div class="table-responsive mt40">
			<table class="table table-bordered">
				<thead>
					<tr>
						<th>ID</th>
						<th>用户组名称</th>
						<th>状态</th>
						<th>添加时间</th>
						<th>更新时间</th>
						<th>操作</th>

					</tr>
				</thead>
				<tbody>
					{foreach $data as $v}
					<tr>
						<td>{$v.id}</td>
						<td>{$v.title}</td>
						<td>
							<?php if($v['id'] != 1){?> {if condition="$v['status'] eq 1"}
							<div id="zt8" onclick="role_state(this,{$v.id})"><span class="label label-info">开启</span></div>
							{else /}
							<div id="zt8" onclick="role_state(this,{$v.id})"><span class="label label-danger">关闭</span></div>
							{/if}
							<?php }?>
						</td>
						<td>{:date('Y-m-d H:i:s',$v.add_time)}</td>
						<td>{:date('Y-m-d H:i:s',$v.update_time)}</td>
						<td>
							<?php if($v['id'] != 1){?>
							<a href="javascript:;" onclick="giveQx(),authority(this)" class="btn btn-info btn-xs" data-authority-id="{$v['id']}"> <i class="fa fa-paste"></i> 权限分配</a>&nbsp;&nbsp;
							<a href="{:url('Authgroup/edit',array('id'=>$v['id']))}" class="btn btn-primary btn-xs"> <i class="fa fa-paste"></i> 编辑</a>&nbsp;&nbsp;
							<!-- <a href="javascript:;" onclick="roleDel(this)" class="btn btn-danger btn-xs"> <i class="fa fa-trash-o"></i> 删除</a> -->
							<?php }?>
						</td>
					</tr>
					{/foreach}
				</tbody>
			</table>
		</div>
		<p class="fr">第1页,共1页</p>
	</div>

	<!-- 角色分配 -->

	<div class="layui-layer layui-layer-page layui-layer-demo layer-anim Role-assignment-tc hides quanxian">
		<div class="layui-layer-title">权限分配</div>
		<div id="" class="layui-layer-content quanxian-content" style="overflow: auto;">
			<div class="left layui-layer-wrap mt20 pl10 " style="" id="">
				<input type="hidden" id="nodeid" value="">
				<h2 class="clearfix mb10"><input class="fl mt2"  type="checkbox" value="" name="allLevel" onclick="allauthority(this)" ><span class="fl">全选</span></h2> {foreach $rule as $key => $val}

				<div class="form-group per_class" data-id="">
					<h2 class="clearfix mb10"><input class="fl mt2"  type="checkbox"  name="oneLevel" onclick="authority(this)" value="{$val.id}"><span class="fl">{$val.title}</span></h2>
					<?php if(!empty( $val['child'])){?> {foreach $val['child'] as $kk => $vv}
					<div class="clearfix pl20 Two_level mb10">
						<h3 class="clearfix fl Two_level_all"><input name="tweLevel" class="fl mt2"  type="checkbox" value="{$vv.id}" onclick="twoLevel(this)"><span class="fl">{$vv.title}：</span></h3>
						<ul class="clearfix fl Three_level">
							<?php if(!empty($vv['levels'])){?> {foreach $vv['levels'] as $k =>$v}
							<li class="fl clearfix ml10"><input name="threeLevel" class="fl" type="checkbox" value="{$v.id}" onclick="threeLevel(this)"><span class="fl">{$v.title}</span></li>
							{/foreach}
							<?php }?>
						</ul>
					</div>
					{/foreach}
					<?php }?>
				</div>
				{/foreach}
				<p class="authority-btn"><input type="button" value="确认分配" class="btn btn-primary" id="postform"></p>

			</div>
		</div>
		<span class="layui-layer-setwin" onclick="roleClose()">
				<a class="layui-layer-ico layui-layer-close layui-layer-close1" href="javascript:;"></a>
			</span>
		<span class="layui-layer-resize"></span>
	</div>
	<script>
		//model高度自适应
		$(document).ready(function() {
			var height = $(window).height() - 120;
			console.log(height)
			$(".quanxian").css("height", height * 0.8);
			$(".quanxian-content").css("height", height * 0.7);
			$('.quanxian').css('top', '100px');
		});

		//删除
		function roleDel(obj) {
			parent.layer.alert('确认删除此条记录吗?', {
				btn: ['确认', '取消'], //按钮

			}, function() {
				parent.layer.open({
					content: '删除成功',
					time: 1
				});
				$(obj).parents("tr").remove()
			});
		}
		//状态开启关闭	
		function role_state(obj, id) {
			$.post("{:url('Authgroup/updateStatus')}", {
				id: id
			}, function(e) {
				if(e.code == '1') {
					parent.layer.msg(e.msg, {
						icon: 1,
						time: 2000
					});
					if($(obj).find("span").hasClass("label-info")) {
						$(obj).find("span").addClass("label-danger")
						$(obj).find("span").removeClass("label-info");
						$(obj).find("span").text("关闭");
					} else {

						$(obj).find("span").removeClass("label-danger")
						$(obj).find("span").addClass("label-info");
						$(obj).find("span").text("开启")

					}
				}
			}, 'json')
		}
		//开启权限分配
		function giveQx() {
			$(".Role-assignment-tc").show()
		}
		//关闭权限分配
		function roleClose() {
			$(".Role-assignment-tc").hide()
		}

		//点击一级
		function allauthority(obj) {
			if($(obj).is(":checked")) {
				$(".per_class").find("input").prop("checked", true)
			} else {
				$(".per_class").find("input").prop("checked", false)
			}
		}

		function authority(obj) {
			var inputAll = $(".per_class").find("input").length;
			if($(obj).is(":checked")) {
				$(obj).parents(".per_class").find("input").prop("checked", true)
			} else {
				$(obj).parents(".per_class").find("input").prop("checked", false)
			}
			var All = 0
			for(var i = 0; i < inputAll; i++) {
				if($(".per_class").find("input").eq(i).is(":checked")) {
					All++
				}
			}
			if(All == inputAll) {
				$("input[name='allLevel']").prop("checked", true)
			} else {
				$("input[name='allLevel']").prop("checked", false)
			}

		}
		//点击三级
		function twoLevel(obj) {
			var tweLevel = $(obj).parents(".per_class").find("input[name='tweLevel']").length;
			var threeLevel = $(obj).parents(".per_class").find("input[name='threeLevel']").length
			var inputAll = $(".per_class").find("input").length;

			if($(obj).is(":checked")) {
				$(obj).parents(".Two_level").find("input[name='threeLevel']").prop("checked", true)
				$(obj).parents(".per_class").find("input[name='oneLevel']").prop("checked", true)
			} else {
				$(obj).parents(".Two_level").find("input[name='threeLevel']").prop("checked", false)
			}

			var threeall = 0
			var threenumAll = 0
			//三级
			for(var j = 0; j < tweLevel; j++) {
				if($(obj).parents(".per_class").find("input[name='tweLevel']").eq(j).is(":checked")) {
					threeall++
				}
			}
			//三级
			for(var i = 0; i < inputAll; i++) {
				if($(".per_class").find("input").eq(i).is(":checked")) {
					threenumAll++
				}
			}

			if(threeall > 0) {
				$(obj).parents(".per_class").find("input[name='oneLevel']").prop("checked", true)
			} else {
				$(obj).parents(".per_class").find("input[name='oneLevel']").prop("checked", false)
			}

			if(threenumAll == inputAll) {
				$("input[name='allLevel']").prop("checked", true)
			} else {
				$("input[name='allLevel']").prop("checked", false)
			}

		}
		//点击四级
		function threeLevel(obj) {
			var thisArry = $(obj).parents("ul").find("li").length;
			var thisAll = $(obj).parents(".per_class").find("input[name='tweLevel']").length;
			var inputAll = $(".per_class").find("input").length;
			var count = 0;
			var count2 = 0;
			var all = 0
			//循环当前三级里面的四级
			for(var i = 0; i < thisArry; i++) {
				if($(obj).parents("ul").find("input").is(":checked")) {
					count++
				}
			}
			//循环当前三级里面的四级
			//for(var i=0; i<thisAll;i++){
			//	if($(obj).parents(".per_class").find("input[name='threeLevel']").is(":checked")){
			//		count2++
			//	}
			//}
			//循环所有的四级
			for(var i = 0; i < inputAll; i++) {
				if($(".per_class").find("input").eq(i).is(":checked")) {
					all++
				}
			}
			//选中三级
			if(count != 0) {
				$(obj).parents(".Two_level").find("input[name='tweLevel']").prop("checked", true)
			}
			//选中三级
			//if(count2!=0){
			//	$(obj).parents(".per_class").find("input[name='oneLevel']").prop("checked",true)
			//}else{
			//	$(obj).parents(".per_class").find("input[name='oneLevel']").prop("checked",false)
			//}
			//选中全选
			if(all == inputAll) {
				$("input[name='allLevel']").prop("checked", true)
			} else {
				$("input[name='allLevel']").prop("checked", false)
			}
		}

		/**
		 * 权限节点回选
		 */

		function authority(obj) {
			var id = $(obj).attr('data-authority-id');
			$('.per_class').attr('data-id', id);
			$.post("{:url('Authgroup/AjaxAuthority')}", {
				id: id
			}, function(e) {
				var inputObj = $('.per_class').find('input');
				$.each(e.data, function(key, val) {
					$.each(inputObj, function(kk, vv) {
						if($(vv).val() == val) {
							$(vv).prop("checked", true);
						}
					})
				})
			}, 'json');
		}

		/* $("#authority").click(function(){
			
		});
		 */

		/** 
		 * 提交
		 */

		$("#postform").click(function() {
			var inputObj = $('.per_class').find('input');
			var rules = [];
			$.each(inputObj, function(key, val) {
				if($(val).is(':checked')) {
					rules.push($(val).val());
				}
			})
			var id = $('.per_class').attr('data-id');
			$.post("{:url('Authgroup/AddRules')}", {
				rules: rules,
				id: id
			}, function(e) {
				if(e.code == 1) {
					top.layer.msg(e.msg, {
						icon: 1,
						time: 2000
					}, function() {
						window.location.href = e.data
					});
				} else {
					top.layer.msg(e.msg, {
						icon: 2,
						time: 2000
					});
				}
			}, 'json')
		})
	</script>
</body>

</html>